// product.js
var WxParse = require('../../wxParse/wxParse.js');  // 富文本解析

import { Config } from '../../utils/config.js';
import { Product } from 'product-model.js';
import { Cart } from '../cart/cart-model.js';

var product = new Product();
var cart = new Cart();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: null,    // 商品id
		countsArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
		productCount: 1,  // 添加商品的数量
		cartTotalCounts: 0,  // 购物车的总数量
		currentTabsIndex: 0,
		product: null,  // 商品详情数据
    producrDetail: '',  // 商品简介富文本
    producrParm: '',  // 商品产品参数
		producrService: '',  // 商品售后保障
    session_3rd:'',
    animationData: {}
	},

	onLoad (options) {
    const that = this
		const id = options.id;
		this.data.id = id;
    // 从缓存中取出3rd_session
    wx.getStorage({
      key: '3rd_session',
      success: function(res) {
        that.setData({
          session_3rd:res.data
        })
        that._loadData();   // 取出session后 加载页面数据
      },
    })
	},
	_loadData () {
		var that = this
		// 获取商品详情数据
		product.getDetailInfo(this.data.id, (res) => {
			this.setData({
				product: res.data
			});
			// 解析富文本
      WxParse.wxParse('producrDetail', 'html', res.data.content, that, 5);  // 详情
      WxParse.wxParse('producrParm', 'html', res.data.parm, that, 5);     // 产品参数
      WxParse.wxParse('producrService', 'html', res.data.service, that, 5);   // 售后保障
		});
		// 获取购物车里之前的总数量
    const session3rd = that.data.session_3rd
    cart.getCartData((data) => {
      // console.log(data.data)
      let cartArr = data.data
      let cartTotalCounts = 0
      if (cartArr.length >= 1){
        cartArr.forEach((item) => {
          cartTotalCounts = cartTotalCounts + item.quantity
        })
        console.log(cartTotalCounts)
        that.setData({
          cartTotalCounts
        })
      }
    }, session3rd)     
	},
	// 选择添加的数量
	bindPickerChange(event) {
		var index = event.detail.value;
		var selectedCount = this.data.countsArray[index]
		this.setData({
			productCount: selectedCount
		});
	},
	// 切换选项卡
	onTabsItemTap (event) {
		var index = product.getDataSet(event, 'index');
		this.setData({
			currentTabsIndex: index
		});
	},
	// 加入购物车
	addToCart () {
		var that = this
		var goods_id = this.data.id     // 被添加商品的id
		var quantity = this.data.productCount   // 被添加商品的数量
		var productCount = this.data.productCount   //  添加商品的数量
		var cartTotalCounts = this.data.cartTotalCounts  // 购物车商品总数量
    var session3rd = this.data.session_3rd
    // var animation = wx.createAnimation({
    //   duration: 200,
    //   timingFunction: 'ease',
    // })
    // this.animation = animation
		// parames
    var options = {
      type: 'post',
      data: {
        session3rd,   
        goods_id,
        quantity
      }
    }
    // 请求添加购物车接口  
    cart.addCart((res) => {
      if (res.status === 1) {
        wx.showToast({
          title: res.data,
          icon: 'success',
          duration: 1500
        })
        // 购物车数量改变 动画
        // animation.scale(2).step()
        // animation.scale(1).step()
        that.setData({
          // animationData: animation.export(),
          cartTotalCounts: cartTotalCounts + productCount   // 更新购物车总数量
        })
      }else{
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: res.msg,
          success: function (res) {
            // console.log(res)
          }
        })
      }
    }, options)
		
	},
	// 购物车页面
	onCartTap (event) {
		wx.switchTab({
			url: '/pages/cart/cart'
		});
	}

})